﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using T2V.SharePoint.Common;
using T2V.SharePoint.Common.Utilities;

namespace T2V.SharePoint.Workflows.TaskActions
{
    public class AdvancePaymentDetail
    {
        public string No { get; set; }
        public string Title { get; set; }
        public string Amount { get; set; }
    }

    public class DeNghiTamUngDataContext : DataContextBase
    {
        public DateTime DateRequest { get; set; }
        public string UserRequest { get; set; }
        public string DepartmentRequest { get; set; }
        public string TypeOfPayment { get; set; }

        public string Chief { get; set; }
        public string ChiefStatus { get; set; }
        public string ChiefComment { get; set; }

        public string Accountant { get; set; }
        public string AccountantStatus { get; set; }
        public string AccountantComment { get; set; }

        public string HeadAccountant { get; set; }
        public string HeadAccountantStatus { get; set; }
        public string HeadAccountantComment { get; set; }

        public string Approver { get; set; }
        public string ApproverStatus { get; set; }
        public string ApproverComment { get; set; }
        
        public string TotalAmount { get; set; }
        public string TotalAmountInLetters { get; set; }
        public string Currency { get; set; }

        public List<ReferenceRequest> ReferenceRequests { get; set; }
        public List<AdvancePaymentDetail> AdvancePaymentDetails { get; set; }

        public DeNghiTamUngDataContext(SPListItem item): base(item)
        {
            if (item["DateRequest"] != null)
                DateRequest = Convert.ToDateTime(item["DateRequest"]);

            if (item["UserRequest"] != null && !string.IsNullOrEmpty(item["UserRequest"].ToString()))
                UserRequest = item["UserRequest"].ToString().Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1];
            else
                UserRequest = string.Empty;

            if (item["DepartmentRequest"] != null)
                DepartmentRequest = item["DepartmentRequest"].ToString();
            else
                DepartmentRequest = string.Empty;

            if (item["TypeOfPayment"] != null)
                TypeOfPayment = item["TypeOfPayment"].ToString();
            else
                TypeOfPayment = string.Empty;

            if (item["Chief"] != null && !string.IsNullOrEmpty(item["Chief"].ToString()))
                Chief = item["Chief"].ToString().Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1];
            else
                Chief = string.Empty;

            if (item["ChiefStatus"] != null)
                ChiefStatus = item["ChiefStatus"].ToString();
            else
                ChiefStatus = string.Empty;

            if (item["ChiefComment"] != null)
                ChiefComment = item["ChiefComment"].ToString();
            else
                ChiefComment = string.Empty;

            if (item["Approver"] != null && !string.IsNullOrEmpty(item["Approver"].ToString()))
                Approver = item["Approver"].ToString().Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1];
            else
                Approver = string.Empty;

            if (item["ApproverStatus"] != null)
                ApproverStatus = item["ApproverStatus"].ToString();
            else
                ApproverStatus = string.Empty;

            if (item["ApproverComment"] != null)
                ApproverComment = item["ApproverComment"].ToString();
            else
                ApproverComment = string.Empty;

            if (item["Accountant"] != null && !string.IsNullOrEmpty(item["Accountant"].ToString()))
                Accountant = item["Accountant"].ToString().Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1];
            else
                Accountant = string.Empty;

            if (item["AccountantStatus"] != null)
                AccountantStatus = item["AccountantStatus"].ToString();
            else
                AccountantStatus = string.Empty;

            if (item["AccountantComment"] != null)
                AccountantComment = item["AccountantComment"].ToString();
            else
                AccountantComment = string.Empty;

            if (item["HeadAccountant"] != null && !string.IsNullOrEmpty(item["HeadAccountant"].ToString()))
                HeadAccountant = item["HeadAccountant"].ToString().Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1];
            else
                HeadAccountant = string.Empty;

            if (item["HeadAccountantStatus"] != null)
                HeadAccountantStatus = item["HeadAccountantStatus"].ToString();
            else
                HeadAccountantStatus = string.Empty;

            if (item["HeadAccountantComment"] != null)
                HeadAccountantComment = item["HeadAccountantComment"].ToString();
            else
                HeadAccountantComment = string.Empty;

            if (item["Currency"] != null)
                Currency = item["Currency"].ToString();
            else
                Currency = string.Empty;

            AdvancePaymentDetails = new List<AdvancePaymentDetail>();
            int total = 0;
            if (item["RequestDetail"] != null)
            {
                SPList listPurchaseDetail = Utility.GetListFromURL(Constants.REQUEST_DETAIL_LIST_URL, item.Web);
                SPFieldLookupValueCollection purchaseDetails = item["RequestDetail"] as SPFieldLookupValueCollection;
                for (int i = 0; i < purchaseDetails.Count; i++ )
                {
                    var purchaseDetail = purchaseDetails[i];
                    SPListItem listItem = listPurchaseDetail.GetItemById(purchaseDetail.LookupId);
                    if (listItem != null)
                    {
                        AdvancePaymentDetail pd = new AdvancePaymentDetail();
                        pd.No = (i + 1).ToString();
                        pd.Title = listItem.Title;

                        int tempAmount = 0;
                        if (listItem["Amount"] != null)
                        {
                            pd.Amount = listItem["Amount"].ToString();
                            int.TryParse(pd.Amount, out tempAmount);
                        }
                        else
                            pd.Amount = "0";

                        total += tempAmount;
                        AdvancePaymentDetails.Add(pd);
                    }
                }
            }
            TotalAmount = total.ToString();
            TotalAmountInLetters = Utility.DocTienBangChu(total, Currency);

            ReferenceRequests = new List<ReferenceRequest>();
            if (item["References"] != null)
            {
                SPList listReferences = item.ParentList;
                SPFieldLookupValueCollection references = item["References"] as SPFieldLookupValueCollection;
                for (int i = 0; i < references.Count; i++)
                {
                    var reference = references[i];
                    SPListItem listItem = listReferences.GetItemById(reference.LookupId);
                    if (listItem != null)
                    {
                        ReferenceRequest rp = new ReferenceRequest();
                        rp.No = (i + 1).ToString();
                        rp.Title = listItem.Title;

                        if (listItem["DateRequest"] != null)
                            rp.DateRequest = Convert.ToDateTime(listItem["DateRequest"]);

                        if (listItem["UserRequest"] != null)
                            rp.UserRequest = listItem["UserRequest"].ToString().Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1];
                        else
                            rp.UserRequest = string.Empty;

                        if (listItem["DepartmentRequest"] != null)
                            rp.DepartmentRequest = listItem["DepartmentRequest"].ToString();
                        else
                            rp.DepartmentRequest = string.Empty;

                        ReferenceRequests.Add(rp);
                    }
                }
            }
        }
    }
}
